'use strict';

const Controller = require('egg').Controller;
//post请求参数 ctx.request.body
//get请求参数 ctx.request.query
class HomeController extends Controller {
  //获取当前游戏用户信息
  async usersList() {
    let rst = {
      status: 200,
      success: true,
      message: ''
    }
    const {ctx} = this;
    ctx.logger.info('请求参数:',ctx.request.query)
    let limit = ctx.request.query.pageSize ? ctx.request.query.pageSize-0 : 10;
    let offset = ctx.request.query.pageNum ? (ctx.request.query.pageNum - 1) * limit : 0;
    try{
      rst.usersList = await ctx.model.User.findAll({
        offset,
        limit,
        order: [['id', 'DESC']]
      })
      //获取该用户游戏角色数量
      let roleNum = 0;
      for(let i = 0; i<rst.usersList.length;i++){
        roleNum = (await rst.usersList[i].getRoles()).length;
        rst.usersList[i] = JSON.parse(JSON.stringify(rst.usersList[i]))
        rst.usersList[i].roleNum = roleNum
      }
      rst.usersTotal = await ctx.model.User.count();
    }catch(err){
      console.log(err)
    }
    ctx.body = rst;
  }
  //获取用户所有角色信息
  async getUserRoles() {
    let rst = {
      status: 200,
      success: true,
      message: ''
    }
    const {ctx} = this;
    ctx.logger.info('请求参数:',ctx.request.query)
    let limit = ctx.request.query.pageSize ? ctx.request.query.pageSize-0 : 10;
    let offset = ctx.request.query.pageNum ? (ctx.request.query.pageNum - 1) * limit : 0;
    try{
      const {count, rows} = await ctx.model.UserInfo.findAndCountAll({
        include:[{
          model: ctx.app.model.User,
          where:{
            account: ctx.request.query.account
          },
          attributes:[],
        }],
        attributes:['id','position_x','position_y','role_name','role_type','scene','sign_up_time','status'],
        offset,
        limit,
        order: [['id', 'DESC']]
      })
    rst.status = 10009;
    rst.roles = rows;
    rst.roleTotal = count;
    }catch(err){
      console.log(err)
    }
    ctx.body = rst;
  }
}

module.exports = HomeController;
